﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace com.dtn.deals.Sercurity
{
    public class Serialization
    {
        public static string SerializeObject(object obj)
        {
            XmlSerializer xs = new XmlSerializer(obj.GetType());
            MemoryStream memo = new MemoryStream();
            StreamWriter writer = new StreamWriter(memo);
            xs.Serialize(writer, obj);
            string result = Encoding.UTF8.GetString(memo.ToArray());
            memo.Close();
            writer.Close();
            return result;
        }
        public static object DeserializeObject(string xmlData, Type type)
        {
            XmlSerializer xs = new XmlSerializer(type);
            byte[] xmlBytes = Encoding.UTF8.GetBytes(xmlData);
            MemoryStream memo = new MemoryStream(xmlBytes);
            object obj = xs.Deserialize(memo);
            memo.Close();
            return obj;
        }
    }
}
